package cn.juforg.gobang.player;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.util.Log;
import cn.juforg.gobang.Chess;
import cn.juforg.gobang.ChessBoard;
import cn.juforg.gobang.interfaces.AIPlayerDao;

public class BaseAI extends BasePlayer implements AIPlayerDao{
	
	public BaseAI(boolean myFlag) {
		super(myFlag);
		Log.i("构造函数", "base ai");
	}
	
	public static Chess bestPoint ;				//最佳落子点（我方最好，敌方最好）	
	{
		bestPoint = new Chess();
	}
	public static List<Chess> bestPointList = new ArrayList<Chess>();
	
	public static int[][] weights ;	//用于存储棋盘权值记录
	
	public static int delyTime =1000;				//电脑计算时间
	
	public static class CalculateRange{
		public static int xstart,xstop,ystart,ystop;
	}//计算范围
	
	public static List<Chess> fiveChess = new ArrayList<Chess>();
	public static List<Chess> forthChess = new ArrayList<Chess>();
	public static List<Chess> doubleThirdChess = new ArrayList<Chess>();
	public static List<Chess> thirdChess = new ArrayList<Chess>();
	
	
	public static Random random = new Random();
	
	int howmu = 0;
	@Override
	public Chess run() {
//		BaseAI.weights[0][0]=-1;
		bestPointList.clear();
	     for (int i = 0; i < ChessBoard.maxX; i++) {
			for (int j = 0; j < ChessBoard.maxY; j++) {
				weights[i][j] = 0;
			}
		}
		for (int i = CalculateRange.xstart; i <= CalculateRange.xstop; i++) {
			for (int j = CalculateRange.ystart; j <= CalculateRange.ystop; j++) {
				if (ChessBoard.chessArray[i][j]=='4') {
					
				weights[i][j]= howWeight(i, j,ChessBoard.PLAYER2FLAG);
				weights[i][j]+= howWeight(i, j,ChessBoard.PLAYER1FLAG);
				if(weights[i][j]>weights[bestPoint.getX()][bestPoint.getY()]){
					bestPoint.setX(i);
					bestPoint.setY(j);
					bestPointList.clear();
					bestPointList.add(bestPoint);
					Log.i("BestPoint", bestPoint.toString());
				}else if (weights[i][j]==weights[bestPoint.getX()][bestPoint.getY()]) {
					bestPointList.add(new Chess(i, j));
				}
				}
			}
		}
		BaseAI.bestPoint=BaseAI.bestPointList.size()>0 ? BaseAI.bestPointList.get(random.nextInt(BaseAI.bestPointList.size())):null;
		return bestPoint;
	}

	public int howInBounch(int x, int y, boolean whoschess) {
		return 0;
	}

	int playerFlag;
    int enemyFlag;
    int count = 0;
    int n = 0;
    int weight = 0;
    boolean halfLife = false;
    boolean deadPoint = false;
    int lifeCount = 0;
     
    private void init() {
	if(deadPoint){
	    count=0;}
	else if(halfLife) {
		if(count<4){count-=2;}
		else if (count>=4) {
		    count*=2;
		}
	}else{
	    weight += count;
	}
            
            halfLife = false;
            deadPoint = false;
            count = 1;
            lifeCount = 0;
           }

	private void deadOrHalfLife(int m, int n, char forword, int palyerFlag) {
		if (halfLife && count < 5) {
   			deadPoint = true;
		} else if(!halfLife ){
		    	halfLife = true;
			isDeadPoint(m, n, forword, 'p', playerFlag);
		}
	}
     public int howWeight(int x, int y, boolean whoschess) {
             playerFlag = whoschess?ChessBoard.BLACK:ChessBoard.WHITE;
             enemyFlag = whoschess?ChessBoard.WHITE:ChessBoard.BLACK;
             weight = 0;
             count = 0;
             halfLife = false;
             deadPoint = false;
             //横向
             init();
             while ((x+count)<=CalculateRange.xstop ) {
             if(ChessBoard.chessArray[x+count][y]==playerFlag||ChessBoard.chessArray[x+count][y]==(playerFlag+2)){
                       count++;
                 }else if(ChessBoard.chessArray[x+count][y]==enemyFlag||ChessBoard.chessArray[x+count][y]==(enemyFlag+2)){
                       halfLife=true;
                       break;
                 }else{
                       break;//没有棋子时
                 }
             }
             for (n=x-1; n >= CalculateRange.xstart ; n--) {
             if(ChessBoard.chessArray[n][y]==playerFlag||ChessBoard.chessArray[n][y]==(playerFlag+2)){
                       count++;
                 }else if(ChessBoard.chessArray[n][y]==enemyFlag||ChessBoard.chessArray[n][y]==(enemyFlag+2)){
                       this.deadOrHalfLife(n,y,'h',playerFlag);
                       /*if (halfLife ) {
               			if(count < 5){
               			deadPoint = true;
               			}
               			}else{
               				halfLife=true;
               			}*/
                       break;
                 }else{
                  isDeadPoint(n,y,'h','n', playerFlag);
//                     if(!halfLife){
//                           for (int i = 0; i < 5; i++) {
//                                 if(ChessBoard.chessArray[n+i][y]=='4'||ChessBoard.chessArray[n+i][y]==playerFlag){
//                                 lifeCount++;
//                                 }
//                           }
//                           if(lifeCount<5){
//                                 deadPoint=true;
//                           }
//                     }
                       break;
                 }
             }
//             Log.i("old_weight:"+playerFlag, "["+x+"]["+y+"]="+weights[x][y]);
             //纵向
             init();
             
             while (y+count<=CalculateRange.ystop) {
                   if(ChessBoard.chessArray[x][y+count]==playerFlag||ChessBoard.chessArray[x][y+count]==(playerFlag+2)){
                             count++;
                       }else if(ChessBoard.chessArray[x][y+count]==enemyFlag||ChessBoard.chessArray[x][y+count]==(enemyFlag+2)){
                             halfLife=true;
                             break;
                       }else{
                             break;
                       }
             }
             for (n=y-1; n >=CalculateRange.ystart ; n--) {
              if(ChessBoard.chessArray[x][n]==playerFlag||ChessBoard.chessArray[x][n]==(playerFlag+2)){
                     count++;
               }else if(ChessBoard.chessArray[x][n]==enemyFlag||ChessBoard.chessArray[x][n]==(enemyFlag+2)){
                     this.deadOrHalfLife(x,n,'z',playerFlag);
                     break;
               }else{
                isDeadPoint(x,n,'z', 'n',playerFlag);
                 break;
               }
             }
             //斜向
             init();
//             Log.i(x+"|"+y+"z", ""+count);
             while ((x+count)<=CalculateRange.xstop && (y-count)>=CalculateRange.ystart ) {
            if (ChessBoard.chessArray[x+count][y-count]==playerFlag || ChessBoard.chessArray[x+count][y-count]== (playerFlag + 2)) {
             count++;
            } else if (ChessBoard.chessArray[x+count][y-count]==enemyFlag || ChessBoard.chessArray[x+count][y-count] == (enemyFlag + 2)) {
             halfLife = true;
             break;
            } else {
             break;
            }
                   }
             while ((x-count)>=CalculateRange.xstart && (y+count)<=CalculateRange.ystop ){
            if(ChessBoard.chessArray[x-count][y+count]==playerFlag || ChessBoard.chessArray[x-count][y+count] == (playerFlag+2)){
                     count++;
               }else if(ChessBoard.chessArray[x-count][y+count] == enemyFlag||ChessBoard.chessArray[x-count][y+count] == (enemyFlag+2)){
                     this.deadOrHalfLife(x-count,y+count,'x',playerFlag);
                     break;
               }else{
                isDeadPoint(x-count,y+count,'x', 'n',playerFlag);
                 break;
               }
             }
             //反斜向
             init();
//             Log.i(x+"|"+y+"x", ""+count);
             while ((x+count)<=CalculateRange.xstop && (y+count)<=CalculateRange.ystop ) {
            if (ChessBoard.chessArray[x+count][y+count]==playerFlag || ChessBoard.chessArray[x+count][y+count]== (playerFlag + 2)) {
		         count++;
		        } else if (ChessBoard.chessArray[x+count][y+count]== enemyFlag || ChessBoard.chessArray[x+count][y+count] == (enemyFlag + 2)) {
		         halfLife = true;
		         break;
		        } else {
		         break;
		        }
             }
             while ((x-count)>=CalculateRange.xstart && (y-count)>=CalculateRange.ystart ){
            if(ChessBoard.chessArray[x-count][y-count]==playerFlag || ChessBoard.chessArray[x-count][y-count] == (playerFlag+2)){
                     count++;
               }else if(ChessBoard.chessArray[x-count][y-count] == enemyFlag || ChessBoard.chessArray[x-count][y-count] == (enemyFlag+2)){
                     this.deadOrHalfLife(x-count,y-count,'f',playerFlag);
                     break;
               }else{
                isDeadPoint(x-count,y-count,'f', 'n',playerFlag);
                 break;
               }
             }
             if(deadPoint){count=0;}
             if(halfLife && count<4){count/=2;}
             weight += count;
             Log.i(x+"|"+y+"f", ""+count);
             Log.i("new_weight:"+playerFlag, "["+x+"]["+y+"]="+weight);
             return weight;
            }

    private void isDeadPoint(int m, int n, char forword ,char pORn,int palyerFlag) {
          lifeCount = 0;
          int bottomx = 0;
          int bottomy = 0;
      switch (forword) {
    case 'h'://横向
          if (halfLife && pORn == 'p') {
                for (int i = 0; i < 6; i++) {
                      if ((m + i)<=BaseAI.CalculateRange.xstop && (ChessBoard.chessArray[m + i][n] == '4' || ChessBoard.chessArray[m + i][n] == playerFlag
                	      || ChessBoard.chessArray[m + i][n] == playerFlag+2)) {
                            lifeCount++;
                      }
                }
                if (lifeCount < 5) {
                      deadPoint = true;
                }
          }else if(pORn == 'n'){
        	  for(int i = 0;m-i>=BaseAI.CalculateRange.xstart && (ChessBoard.chessArray[m - i][n] == '4' || ChessBoard.chessArray[m - i][n] == playerFlag
        	  || ChessBoard.chessArray[m-i][n] == (playerFlag+2));i++){
        		  bottomx= m-i;
        	  }
        	  for(int i = 0;lifeCount <= 6 && bottomx+i<=BaseAI.CalculateRange.xstop && (ChessBoard.chessArray[bottomx+i][n] == '4' || ChessBoard.chessArray[bottomx+i][n] == playerFlag
        	  || ChessBoard.chessArray[bottomx+i][n] == (playerFlag+2));i++){
        		  lifeCount++;
        	  }
        	  if (lifeCount < 5) {
                  deadPoint = true;
        	  }
          }
          
          break;
    case 'z'://纵向
    	 if (halfLife && pORn == 'p') {
             for (int i = 0; i < 6; i++) {
                   if ((n + i)<=BaseAI.CalculateRange.ystop && (ChessBoard.chessArray[m][n+i] == '4' || ChessBoard.chessArray[m][n+i] == playerFlag
                	   || ChessBoard.chessArray[m][n+i] == playerFlag+2)) {
                         lifeCount++;
                   }
             }
             if (lifeCount < 5) {
                   deadPoint = true;
             }
       }else if(pORn == 'n'){
     	  for(int i = 0;n-i>=BaseAI.CalculateRange.ystart && (ChessBoard.chessArray[m][n- i] == '4' || ChessBoard.chessArray[m][n - i] == playerFlag
     	  || ChessBoard.chessArray[m][n-i] == (playerFlag+2));i++){
     		  bottomy= n-i;
     	  }
     	  for(int i = 0; lifeCount<=6 && bottomy+i<=BaseAI.CalculateRange.ystop && (ChessBoard.chessArray[m][bottomy+i] == '4' || ChessBoard.chessArray[m][bottomy+i] == playerFlag
     	  || ChessBoard.chessArray[m][bottomy+i] == (playerFlag+2));i++){
     		  lifeCount++;
     	  }
     	  if (lifeCount < 5) {
               deadPoint = true;
     	  }
       }
          break;
    case 'x'://斜向
    	 if (halfLife && pORn == 'p') {
             for (int i = 0; i < 6; i++) {
                   if ((m + i)<=BaseAI.CalculateRange.xstop && (n - i)>=BaseAI.CalculateRange.ystart && (ChessBoard.chessArray[m+i][n-i] == '4' || ChessBoard.chessArray[m+i][n-i] == playerFlag
                		   || ChessBoard.chessArray[m+i][n-i] == (playerFlag+2))) {
                         lifeCount++;
                   }
             }
             if (lifeCount < 5) {
                   deadPoint = true;
             }
       }else if(pORn == 'n'){
     	  for(int i = 0;m-i>=BaseAI.CalculateRange.xstart && n+i<=BaseAI.CalculateRange.ystop && (ChessBoard.chessArray[m-i][n+i] == '4' || ChessBoard.chessArray[m-i][n+i] == playerFlag
     	  || ChessBoard.chessArray[m-i][n+i] == (playerFlag+2));i++){
     		  bottomx= m-i;
     		  bottomy = n+i;
     	  }
	    for(int i = 0; lifeCount<=6 && bottomx+i<=BaseAI.CalculateRange.xstop && bottomy-i>=BaseAI.CalculateRange.ystart && (ChessBoard.chessArray[bottomx+i][bottomy-i] == '4' || ChessBoard.chessArray[bottomx+i][bottomy-i] == playerFlag
	      || ChessBoard.chessArray[bottomx+i][bottomy-i] == (playerFlag+2));i++){
	    	  lifeCount++;
	      }
     	  if (lifeCount < 5) {
               deadPoint = true;
     	  }
       }
          break;
          
    case 'f'://反斜向
    	 if (halfLife && pORn == 'p') {
             for (int i = 0; i < 6; i++) {
                   if ((m + i)<=BaseAI.CalculateRange.xstop && (n + i)<=BaseAI.CalculateRange.ystop && (ChessBoard.chessArray[m+i][n+i] == '4' || ChessBoard.chessArray[m+i][n+i] == playerFlag
                	   || ChessBoard.chessArray[m+i][n+i] == (playerFlag+2))) {
                         lifeCount++;
                   }
             }
             if (lifeCount < 5) {
                   deadPoint = true;
             }
       }else if(pORn == 'n'){
     	  for(int i = 0;m-i>=BaseAI.CalculateRange.xstart && n-i>=BaseAI.CalculateRange.ystart && (ChessBoard.chessArray[m-i][n- i] == '4' || ChessBoard.chessArray[m-i][n - i] == playerFlag
     	  || ChessBoard.chessArray[m-i][n-i] == (playerFlag+2));i++){
     		  bottomx= m-i;
     		  bottomy = n -i;
     	  }
     	  for(int i = 0; lifeCount<=6 && bottomx+i<=BaseAI.CalculateRange.xstop && bottomy+i<=BaseAI.CalculateRange.ystop && (ChessBoard.chessArray[bottomx+i][bottomy+i] == '4' || ChessBoard.chessArray[bottomx+i][bottomy+i] == playerFlag
     	  || ChessBoard.chessArray[bottomx+i][bottomy+i] == (playerFlag+2));i++){
     		  lifeCount++;
     	  }
     	  if (lifeCount < 5) {
               deadPoint = true;
     	  }
       }
          break;
    }
     } 



	
	
}
